<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               v-model:page="page"
               v-model:search="search"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @sort-change="sortChange"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template #menu-left>
        <el-button
                type="primary"
                icon="el-icon-plus"
                v-if="permission.material_no_add"
                @click="handleAdd"
        >新 增
        </el-button>
        <el-button type="danger"
                   icon="el-icon-delete"
                   v-if="permission.material_no_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template #createTime-search="{ disabled, size }">
        <!--<div style="display: flex;flex-direction: start;">-->
          <!--<el-select v-model="search.createTimeRule" placeholder="比较" filterable clearable>-->
            <!--<el-option label="" value=""></el-option>-->
            <!--<el-option value=">" label="大于">大于 </el-option>-->
            <!--<el-option value=">=" label="大于等于">大于等于 </el-option>-->
            <!--<el-option value="=" label="等于">等于 </el-option>-->
            <!--<el-option value="!=" label="不等于">不等于 </el-option>-->
            <!--<el-option value="<=" label="小于等于">小于等于 </el-option>-->
            <!--<el-option value="<" label="小于">小于 </el-option>-->
          <!--</el-select>-->
          <!--<el-date-picker-->
                  <!--size="default"-->
                  <!--v-model="search.createTimeSearch"-->
                  <!--type="date"-->
                  <!--:format="dateFormat"-->
                  <!--:value-format="dateFormat"-->
                  <!--:unlink-panels="true"-->
                  <!--clearable-->
                  <!--placeholder="创建日期"-->

          <!--&gt;-->
          <!--</el-date-picker>-->
        <!--</div>-->
        <el-date-picker
                size="default"
                style="width:94%"
                v-model="this.createTimeRange"
                type="daterange"
                :format="dateFormat"
                :value-format="dateFormat"
                unlink-panels
                start-placeholder="Start Date"
                end-placeholder="End Date"
                clearable
                placeholder="创建日期"
                @change="handleDatePickerChange"
                :picker-options="pickerOptions"
        >
        </el-date-picker>
      </template>

      <template #menu="{ row, size, type }">
        <el-tooltip content="查看" placement="top">
          <el-button
                  icon="el-icon-view"
                  link
                  :size="size"
                  :type="type"
                  @click="() => handleView(row)"
                  v-if="permission.material_no_view"
          >
          </el-button>
        </el-tooltip>
        <el-tooltip content="修改" placement="top">
          <el-button
                  icon="el-icon-edit"
                  link
                  :size="size"
                  :type="type"
                  @click="() => handleEdit(row)"
                  v-if="permission.material_no_edit"
          >
          </el-button>
        </el-tooltip>
        <el-tooltip content="删除" placement="top">
          <el-button
                  icon="el-icon-delete"
                  link
                  :size="size"
                  :type="type"
                  @click="() => rowDel(row)"
                  v-if="permission.material_no_delete"
          >
          </el-button>
        </el-tooltip>
      </template>
    </avue-crud>
      <material-no-form ref="materialNoForm" @success="refreshChange"></material-no-form>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/anwise/material/material_no";
  import {mapGetters} from "vuex";
  import MaterialNoForm from "@/views/anwise/material/material_no_form.vue";

  export default {
      components: {
          MaterialNoForm,
      },
    data() {
      return {
          form: {},
          query: {},
          createTimeRange:[],
          loading: true,
          dateFormat: 'YYYY-MM-DD',
          pickerOptions: {},
          page: {
              pageSize: 20,
              currentPage: 1,
              total: 0
          },
          search:{},
          selectionList: [],
          option: {
              height:'auto',
              calcHeight: 30,
              tip: false,
              searchShow: false,
              searchMenuSpan: 6,
              border: true,
              index: true,
              selection: true,
              reserveSelection:true,
              labelWidth: 150,
              indexWidth:100,
              menuWidth: 120,
              addBtn: false,
              editBtn: false,
              viewBtn: false,
              delBtn: false,
              stripe: true,
              searchIndex: 3,
              searchIcon: false,
              searchEnter:true,
          column: [
            {
              label: "",
              prop: "id",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
                overHidden: true,
                sortable: true,
                hide:true,
            },
            {
              label: "编号代码",
              prop: "codeNo",
              type: "input",
              search: true,
              overHidden: true,
              sortable: true,
              rules: [{
                required: true,
                message: "请输入编号代码",
                trigger: "blur"
              }],
            },
            {
              label: "编号名称",
              prop: "codeName",
              type: "input",
              search: true,
                overHidden: true,
                sortable: true,
              rules: [{
                required: true,
                message: "请输入编号名称",
                trigger: "blur"
              }],
            },
            {
              label: "备注",
              prop: "remark",
              type: "textarea",
                overHidden: true,
                sortable: true,
              span: 24,
            },
            {
              label: "创建人",
              prop: "createUserName",
              type: "input",
                overHidden: true,
                sortable: true,
            },
            {
              label: "创建时间",
              prop: "createTime",
              type: "input",
                overHidden: true,
                sortable: true,
                search:true,
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.validData(this.permission.material_no_add, false),
          viewBtn: this.validData(this.permission.material_no_view, false),
          delBtn: this.validData(this.permission.material_no_delete, false),
          editBtn: this.validData(this.permission.material_no_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
        handleView(row) {
            let title="查看物料编号";
            let type='view';
            const id = row.id;
            this.$refs.materialNoForm.show({
                id,
                title,
                type
            })
        },
        handleEdit(row) {
            let title="编辑物料编号";
            let type='edit';
            const id = row.id;
            this.$refs.materialNoForm.show({
                id,
                title,
                type
            })
        },
        handleAdd() {
            let title="创建物料编号";
            let type='edit';
            this.$refs.materialNoForm.show({
                title,
                type
            })
        },
        sortChange(val) {
            console.log(val)
            this.sort = {
                prop: val.prop,
                order: val.order
            };
            let order = "";
            if (this.sort.prop && this.sort.order) {
                if (this.sort.order === "descending") {
                    order = `${this.sort.prop} desc`
                } else {
                    order = `${this.sort.prop} asc`
                }
            }
            this.query.orderBy = order;
            this.onLoad(this.page);
        },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
          this.createTimeRange = [];
          this.search.createTimeStart = '';
          this.search.createTimeEnd = '';
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      },
        handleDatePickerChange(value) {
            console.log(value);
            if (value) {
                this.search.createTimeStart = value[0];
                this.search.createTimeEnd = value[1];
            } else {
                this.search.createTimeStart = '';
                this.search.createTimeEnd = '';
            }
        },
    }
  };
</script>

<style>
</style>
